|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
var _ = require('lodash'), |
|
4
|
|
|
Promise = require('bluebird'), |
|
|
|
|
|
|
5
|
|
|
Move = require('./move'), |
|
6
|
|
|
Position = require('./position'), |
|
7
|
|
|
Grid = require('./grid'); |
|
8
|
|
|
|
|
9
|
|
|
// Expose `Mower` |
|
10
|
|
|
|
|
11
|
|
|
module.exports = Mower; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Set up Mower with `options` |
|
15
|
|
|
* |
|
16
|
|
|
* Parameters: |
|
17
|
|
|
* |
|
18
|
|
|
* - `id` a valid number |
|
19
|
|
|
* - `grid` a valid Grid object |
|
20
|
|
|
* - `position` a valid Position object |
|
21
|
|
|
* - `instructions` a valid Array that contains only the following instructions : 'A', 'G' and 'D'. |
|
22
|
|
|
* |
|
23
|
|
|
* @param {Number} id |
|
24
|
|
|
* @param {Object} grid |
|
25
|
|
|
* @param {Object} position |
|
26
|
|
|
* @param {Array} instructions |
|
27
|
|
|
* @api public |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
function Mower(id, grid, position, instructions) { |
|
31
|
|
|
this.id = id; |
|
32
|
|
|
this.grid = grid; |
|
33
|
|
|
this.position = position; |
|
34
|
|
|
this.instructions = instructions; |
|
35
|
|
|
this.move = new Move(this, grid); |
|
36
|
|
|
|
|
37
|
|
|
this.isValid(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Start mower's instructions |
|
42
|
|
|
* |
|
43
|
|
|
* @api public |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
Mower.prototype.start = function() { |
|
47
|
|
|
var self = this; |
|
48
|
|
|
return new Promise(function(resolve) { |
|
49
|
|
|
self.runStep(0, resolve); // run first step |
|
50
|
|
|
}); |
|
51
|
|
|
}; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Types validator |
|
55
|
|
|
* |
|
56
|
|
|
* @api public |
|
57
|
|
|
*/ |
|
58
|
|
|
|
|
59
|
|
|
Mower.prototype.isValid = function() { |
|
60
|
|
|
if(!_.isInteger(this.id)) { |
|
61
|
|
|
throw new Error('Id is not valid'); |
|
62
|
|
|
} |
|
63
|
|
|
if(!isGrid(this.grid)) { |
|
64
|
|
|
throw new Error('Grid is not valid'); |
|
65
|
|
|
} |
|
66
|
|
|
if(!isPosition(this.position)) { |
|
67
|
|
|
throw new Error('Position is not valid'); |
|
68
|
|
|
} |
|
69
|
|
|
if(!isInstructions(this.instructions)) { |
|
70
|
|
|
throw new Error('Instructions are not valid'); |
|
71
|
|
|
} |
|
72
|
|
|
}; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Run one step then pass to next instruction or resolve promise when instructions are finished |
|
76
|
|
|
* |
|
77
|
|
|
* @param {Number} step |
|
78
|
|
|
* @param {Function} end |
|
79
|
|
|
* @api public |
|
80
|
|
|
*/ |
|
81
|
|
|
|
|
82
|
|
|
Mower.prototype.runStep = function(step, end) { |
|
83
|
|
|
var self = this; |
|
84
|
|
|
this.move.runInstruction(this.instructions[step]).then(function() { |
|
85
|
|
|
var nexStep = step+1; |
|
86
|
|
|
if(nexStep >= 0 && nexStep <= self.instructions.length-1){ |
|
87
|
|
|
self.runStep(nexStep, end); |
|
88
|
|
|
} |
|
89
|
|
|
else { |
|
90
|
|
|
end(self); |
|
91
|
|
|
} |
|
92
|
|
|
}) |
|
93
|
|
|
}; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Grid validator helper |
|
97
|
|
|
* |
|
98
|
|
|
* @param {Object} grid |
|
99
|
|
|
* @api protected |
|
100
|
|
|
*/ |
|
101
|
|
|
|
|
102
|
|
|
function isGrid(grid) { |
|
103
|
|
|
return grid instanceof Grid; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Position validator helper |
|
108
|
|
|
* |
|
109
|
|
|
* @param {Object} position |
|
110
|
|
|
* @api protected |
|
111
|
|
|
*/ |
|
112
|
|
|
|
|
113
|
|
|
function isPosition(position) { |
|
114
|
|
|
return position instanceof Position; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Instructions validator helper |
|
119
|
|
|
* |
|
120
|
|
|
* @param {Object} instructions |
|
121
|
|
|
* @api protected |
|
122
|
|
|
*/ |
|
123
|
|
|
|
|
124
|
|
|
function isInstructions(instructions) { |
|
125
|
|
|
return _.isArray(instructions); |
|
126
|
|
|
} |